//
//  TopicViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/26.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "TopicViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "TopicDetailModel.h"
#import "TopicCell.h"
#import "CartoonRecommendDetailViewController.h"

@interface TopicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation TopicViewController

- (void)dealloc
{
    [_introString release];
    [_collect release];
    [_topicArray release];
    [_name release];
    [_myId release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.topicArray = [NSMutableArray array];

    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.name;
    [self startConnection];
    [self createCollectionView];
    
    //设置返回按钮
    UIButton *left = [UIButton buttonWithType:UIButtonTypeSystem];
    left.frame = CGRectMake(0, 0, 32, 32);
    [left setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 
#pragma mark 数据请求
- (void)startConnection
{
    NSString *str = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=listByType&id=%@&page=1&limit=1000", self.myId];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        self.introString = [dic objectForKey:@"intro"];
        NSArray *array = [dic objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            TopicDetailModel *model = [[TopicDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.topicArray addObject:model];
        }
        [self.collect reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

#pragma mark -
#pragma mark 创建collectionView
- (void)createCollectionView
{

    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    flow.itemSize = CGSizeMake(self.view.frame.size.width - 20, 150);
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];

    self.collect.dataSource = self;
    self.collect.delegate = self;
    [self.collect setBackgroundColor:[UIColor clearColor]];
    [self.collect registerClass:[TopicCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.collect];
    [self.collect release];
}

#pragma mark -
#pragma mark collectionView协议

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.topicArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TopicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    TopicDetailModel *model = [self.topicArray objectAtIndex:indexPath.item];
    [cell.showImage sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    cell.nameLabel.text = model.name;
    if ([model.is_over isEqualToString:[NSString stringWithFormat:@"1"]]) {
        cell.curNumLabel.text = [NSString stringWithFormat:@"已完结 全%@集", model.cur_num];
    }
    if ([model.is_over isEqualToString:[NSString stringWithFormat:@"0"]]) {
        cell.curNumLabel.text = [NSString stringWithFormat:@"更新至%@集", model.cur_num];
    }
    cell.scoreLabel.text = [NSString stringWithFormat:@"%@分", model.score];
    cell.topicIntroLabel.text = model.topic_intro;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonRecommendDetailViewController *detail = [[CartoonRecommendDetailViewController alloc] init];
    TopicDetailModel *model = [self.topicArray objectAtIndex:indexPath.item];
    detail.iD = model.video_id;
    detail.lastup = model.cur_num;
    detail.titleName = model.name;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}

@end
